package com.acme.bankapp.domain.bank;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.acme.bankapp.service.bank.AbstractClientListener;
import com.acme.bankapp.service.bank.BankApplication;

/**
 * @author Andrey Alekov Bank class.
 */

public class Bank {
	final static Logger logger = LoggerFactory.getLogger(BankApplication.class);
	private String bankName;
	private Client[] clients = new Client[10];
	private AbstractClientListener[] listeners = new AbstractClientListener[2];

	public Bank(String bankName) {
		this.setBankName(bankName);
		class PrintClientListener extends AbstractClientListener {
			@Override
			public void onClientAdded(Client client) {
				logger.info(client.toString());
			}
		}
		final class EmailNotificationListener extends AbstractClientListener {
			@Override
			public void onClientAdded(Client client) {
				logger.info("Notification email to " + client.toString()
						+ " to be sent.");
			}
		}
		this.listeners[0] = new PrintClientListener();
		this.listeners[1] = new EmailNotificationListener();

	}

	public Bank(String bankName, AbstractClientListener[] listeners) {
		this.setBankName(bankName);
		this.listeners = listeners;
	}

	public Client[] getClients() {
		return this.clients;
	}

	public String getBankName() {
		return bankName;
	}

	public void setBankName(String bankName) {
		this.bankName = bankName;
	}

	public boolean addClient(Client client) {
		for (int i = 0; i < clients.length; i++) {
			if (clients[i] == null) {
				clients[i] = client;
				for (ClientRegistrationListener listener : listeners) {
					listener.onClientAdded(client);
				}
				return true;
			}
		}
		return false;
	}

	public void printMaximumAmountToWithdraw() {
		for (Client client : clients) {
			if (client != null) {
				logger.info("Client: " + client.getName() + " ; "
						+ client.getAccount().maximumAmountToWithdraw());
			}
		}
	}
}
